package com.example.practica1;

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.Toast;

public class Activity2 extends Activity {
	int aleat = 0;
	int aux = 0;
	int jug;
	String name1 = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity1);

		Bundle b = this.getIntent().getExtras();
		jug = b.getInt("Jugador");
		name1 = b.getString("NombreJug1").toString();

		Toast toast = Toast.makeText(this, "Turno de la máquina",
				Toast.LENGTH_SHORT);

		toast.show();

		Toast toast2 = Toast.makeText(this, "Máquina pensando",
				Toast.LENGTH_SHORT);

		toast2.show();

		//		ImageButton resultado = (ImageButton) findViewById(R.id.resultado);
		// resultado.setOnLongClickListener(new View.OnLongClickListener() {

		// @Override
		// public boolean onLongClick(View v) {
		// Intent intent = new Intent(Activity2.this, Activity3.class);
		// intent.putExtra("Maquina", aux);
		// intent.putExtra("Jugador", jug);
		// startActivity(intent);
		// return false;
		// }
		// });

		Thread t = new Thread(new Runnable() {
			public void run() {

				// Aquí ejecutamos nuestras tareas costosas

				final ImageButton resultado = (ImageButton) findViewById(R.id.resultado);

				for (int i = 0; i < 10; i++) {
					aleat = (int) (Math.random() * 5 + 1);
					System.out.println("Ale " + aleat);

					switch (aleat) {
					case 1:
						resultado.post(new Runnable() {
							public void run() {
								resultado.setImageDrawable(getResources()
										.getDrawable(R.drawable.piedra));
								resultado.setVisibility(1);
							}
						});

						break;

					case 2:
						resultado.post(new Runnable() {
							public void run() {
								resultado.setImageDrawable(getResources()
										.getDrawable(R.drawable.papel));
								resultado.setVisibility(1);
							}
						});

						break;
					case 3:

						resultado.post(new Runnable() {
							public void run() {
								resultado.setImageDrawable(getResources()
										.getDrawable(R.drawable.tijeras));
								resultado.setVisibility(1);
							}
						});

						break;
					case 4:
						resultado.post(new Runnable() {
							public void run() {
								resultado.setImageDrawable(getResources()
										.getDrawable(R.drawable.lagarto));
								resultado.setVisibility(1);
							}
						});

						break;
					case 5:
						resultado.post(new Runnable() {
							public void run() {
								resultado.setImageDrawable(getResources()
										.getDrawable(R.drawable.spok));
								resultado.setVisibility(1);
							}
						});

						break;

					}

					aux = aleat;
					try {
						Thread.sleep(400);
					} catch (InterruptedException e) {
					}

					if (i == 9) {
						Intent intent = new Intent(Activity2.this,
								Activity3.class);
						intent.putExtra("Maquina", aux);
						intent.putExtra("Jugador", jug);
						intent.putExtra("NombreJug1", name1);
						startActivity(intent);

					}
				}
				System.out.println("AUX!!!!! " + aux);

			}

		});
		t.start();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity2, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		Toast toast = Toast.makeText(this, "TRAMPOSO!!!", Toast.LENGTH_SHORT);

		toast.show();

		ToneGenerator sound = new ToneGenerator(AudioManager.STREAM_SYSTEM, 100);
		sound.startTone(ToneGenerator.TONE_PROP_BEEP);

		Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

		vibrator.vibrate(500);
	}

	@Override
	protected void onRestart() {
		finish();
		super.onRestart();
	}

	
}
